function addReply() {
    // Check that we're on the correct page
    var letter;
    var reply = localStorage.getItem ("po_msg_reply");
    if (reply == null) return;

    var input = document.sendform.textarea; //document.getElementById("textarea");
    if (input)
        input.value = reply;
    localStorage.removeItem ("po_msg_reply");
}

function addMassPM (){
    var form = document.sendform;
    var name = form.recipient;
    var td2 = name.parentNode;
    var row = td2.parentNode;
    var td1 = row.cells[0];
    
    td1.innerHTML += "<a id='po_mass_pm_switch' mass='off' style='color:lime;text-weight:bold' href='javascript:switchMassPM();void(0)'>+/++</a>";
    
    var td = document.createElement ("td");
    td.innerHTML = "<td><input id='mass_pm_recipients' size='60' title='Enter names separated by comma. E.g. Tatorius,James Brown,Sisi. Do NOT enter any extra spaces!'></input><br>\
                   <input id='mass_pm_cc' type='checkbox'>Include CC</input><br>\
                    <a style='color:lime;text-weight:bold;' href='javascript:sendMassPM();void(0)'>Send Mass PM now</a></td>";
    td.style.display = "none";
    row.appendChild (td);
}

function sendMassPM() {
    var input = document.getElementById("mass_pm_recipients");
    if (!input) return;
    var cc = document.getElementById ("mass_pm_cc").checked;
    
    var rec = input.value;
    if (rec == null || rec.length == 0) {
        input.value = "Enter names separated by comma. E.g. Tatorius,James Brown,Sisi. Do NOT enter any extra spaces!";
        return;
    } else {
        var subj = document.sendform.textfield.value; //document.getElementById ("subjectInput").value;
        var txt = document.sendform.textarea.value; //document.getElementById ("textarea").value;
        if (cc) txt = "CC:" + rec + "\n\n" + txt;
        rec = rec.split (",");
        var sign = document.getElementById ("attach_signature").checked;
        for (i=0; i<rec.length; i++) {
            var pname = rec[i].replace(/^\s\s*/, '').replace(/\s\s*$/, '');//.replace (/\s/g,'%20');
            POsendMsg (pname, subj, txt, sign);
        }
        window.close();
    }
}

function switchMassPM() {
    var link = document.getElementById ('po_mass_pm_switch');
    if (!link) return;
    var form = document.sendform;
    var name = form.recipient;
    var td2 = name.parentNode;
    var td3 = td2.parentNode.cells[2];
    var td4 = td2.parentNode.cells[3];
    var btn = document.sendform.Send; //document.getElementById ("Send");
    
    if (link.getAttribute ("mass") == "off") {
        // Mass-PM mode is off so we need to switch it on
        link.setAttribute ("mass", "on");
        td2.style.display = "none";
        td3.style.display = "none";
        td4.style.display = "block";
        btn.style.display = "none";
    } /*else {
        // Mass-PM mode is on so we need to switch it off
        link.setAttribute ("mass", "off");
        td2.style.display = "block";
        td3.style.display = "block";
        td4.style.display = "none";
        btn.style.display = "block";
    }*/
}

addReply();
addMassPM();